using Core.Ndatax.Domain;
using Core.Ndatax;
using Core.Ndatax.Infrastructure.DBContext;
using Core.Ndatax.Services;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NLog.Web;
using SoapCore;
using System.ServiceModel.Channels;
using Xman.ActivityId.Http;
using Xman.Dapper.Sqlserver;


var builder = WebApplication.CreateBuilder(args);
builder.Host.UseNLog();
builder.Services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true).AddNewtonsoftJson();

// Add services to the container.
builder.Services.TryAddSingleton<IESBService, ESBService>();
builder.Services.AddSingleton<IBllRequest, BllRequest>();
builder.Services.AddSwaggerGen();
builder.Services.AddSoapCore();
builder.Services.AddDapperContext<ReservationDapper, ReservationDapperOptions>(t => t.Connstr = builder.Configuration["DBConnect:DBconn"]);
builder.Services.AddHttpClient<ResServer>(t => t.Timeout = TimeSpan.FromSeconds(30));
builder.Services.AddHttpClient<RisServer>(t => t.Timeout = TimeSpan.FromSeconds(30));

builder.Services.Configure<UploadOptions>(builder.Configuration.GetSection(UploadOptions.SectionName));
builder.Services.Configure<RisOptions>(builder.Configuration.GetSection(RisOptions.SectionName));

builder.Services.AddHostedService<Worker>();

var app = builder.Build();

app.UseNLogActivityId();
app.UseSwagger();
app.UseSwaggerUI();
app.MapControllers();
app.UseRouting();

var settings = builder.Configuration.GetSection("FileWSDL").Get<WsdlFileOptions>();
settings.AppPath = builder.Environment.ContentRootPath; 

app.UseEndpoints(endpoints => {
    endpoints.UseSoapEndpoint<IESBService>("/ESB.asmx", new SoapEncoderOptions()
    {
        MessageVersion = MessageVersion.Soap12WSAddressingAugust2004
    }, SoapSerializer.DataContractSerializer, true, null, settings);
});

app.Run();
